package com.github.cakin.shiro.chapter17.web.controller;

import com.github.cakin.shiro.chapter17.entity.Client;
import com.github.cakin.shiro.chapter17.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @className: ClientController
 * @description: 客户端管理控制器
 * @date: 2020/5/27
 * @author: cakin
 */
@Controller
@RequestMapping("/client")
public class ClientController {
    /**
     * 注入客户端服务
     */
    @Autowired
    private ClientService clientService;

    /**
     * 功能描述：列式所有客户端信息
     *
     * @param model 返回给客户端的数据
     * @return String 客户端展示页面
     * @author cakin
     * @date 2020/5/27
     */
    @RequestMapping(method = RequestMethod.GET)
    public String list( Model model ) {
        model.addAttribute("clientList", clientService.findAll());
        return "client/list";
    }

    /**
     * 功能描述：创建一个客户端
     *
     * @param model 返回给客户端的数据
     * @return String 客户端展示页面
     * @author cakin
     * @date 2020/5/27
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String showCreateForm( Model model ) {
        model.addAttribute("client", new Client());
        model.addAttribute("op", "新增");
        return "client/edit";
    }

    /**
     * 功能描述：创建一个客户端
     *
     * @param client             待保存的客户端数据
     * @param redirectAttributes 重定向属性
     * @return String 重定向页
     * @author cakin
     * @date 2020/5/27
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String create( Client client, RedirectAttributes redirectAttributes ) {
        clientService.createClient(client);
        redirectAttributes.addFlashAttribute("msg", "新增成功");
        return "redirect:/client";
    }

    /**
     * 功能描述：更新一个客户端
     *
     * @param id    客户端id
     * @param model 返回给客户端的数据
     * @return String 客户端修改展示页面
     * @author cakin
     * @date 2020/5/27
     */
    @RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
    public String showUpdateForm( @PathVariable("id") Long id, Model model ) {
        model.addAttribute("client", clientService.findOne(id));
        model.addAttribute("op", "修改");
        return "client/edit";
    }

    /**
     * 功能描述：更新一个客户端
     *
     * @param client             待更新的客户端数据
     * @param redirectAttributes 重定向属性
     * @return String 重定向页
     * @author cakin
     * @date 2020/5/27
     */
    @RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
    public String update( Client client, RedirectAttributes redirectAttributes ) {
        clientService.updateClient(client);
        redirectAttributes.addFlashAttribute("msg", "修改成功");
        return "redirect:/client";
    }

    /**
     * 功能描述：删除一个客户端
     *
     * @param id    客户端id
     * @param model 返回给客户端的数据
     * @return String 客户端删除展示页面
     * @author cakin
     * @date 2020/5/27
     */
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public String showDeleteForm( @PathVariable("id") Long id, Model model ) {
        model.addAttribute("client", clientService.findOne(id));
        model.addAttribute("op", "删除");
        return "client/edit";
    }

    /**
     * 功能描述：删除一个客户端
     *
     * @param id                 客户端id
     * @param redirectAttributes 重定向属性
     * @return String 重定向页
     * @author cakin
     * @date 2020/5/27
     */
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    public String delete( @PathVariable("id") Long id, RedirectAttributes redirectAttributes ) {
        clientService.deleteClient(id);
        redirectAttributes.addFlashAttribute("msg", "删除成功");
        return "redirect:/client";
    }
}
